import { App } from "../../../../src/App";
import { RedPointEvent } from "../../../../src/core/EventConst";
import { UserData } from "../../../src/BaseModule";
import MailSys from "../../../src/system/MailSys";
import PackSys from "../../../src/system/PackSys";
import RedPointSys from "../../../src/system/RedPointSys";
import Calendar from "../../src/Component/Calendar";
import { GameModule, LandlordEvents } from "../Game/GameModule";

const { menu, ccclass, property } = cc._decorator;
@ccclass
@menu("斗地主/大厅场景")
export default class HallScene extends cc.Component {

    @property(cc.Label)
    lblName: cc.Label = null;
    @property(cc.Label)
    lblID: cc.Label = null;
    @property(cc.Label)
    lblGold: cc.Label = null;
    @property(cc.Label)
    lblRank: cc.Label = null;
    @property(cc.Sprite)
    imgHead: cc.Sprite = null;
    @property(cc.Graphics)
    QRCodeNode: cc.Graphics = null;
    @property(cc.Node)
    calendarNode: cc.Node = null;
    @property(cc.Prefab)
    calendarPrefab: cc.Prefab = null;
    @property(cc.RichText)
    lblTips: cc.RichText = null;

    onLoad(){
        App.Event.on(LandlordEvents.EC_UPDATE_USER_INFO,(data:UserData)=>{
            App.Avatar.setHead(data.Avatar, this.imgHead);
            this.lblGold.string = `${App.Util.formatScore(data.Coin)}`;
            this.lblID.string = GameModule.userTitle(data.Coin);
            App.setLableStr(this.lblName,data.UserName,'...',12);
        },this);
    }

    start() {
        App.Audio.randomPlayDir(['music/bgm00:room','music/bgm01:room','music/bgm02:room','music/bgm03:room']);
        let data = GameModule.userInfo;
        App.Avatar.setHead(data.Avatar, this.imgHead);
        this.lblGold.string = `${App.Util.formatScore(data.Coin)}`;
        this.lblID.string = GameModule.userTitle(data.Coin);
        App.setLableStr(this.lblName,data.UserName,'...',12);

        const str = App.getText(GameModule.isSingle ?  'landlord.watch_viedo0' : 'landlord.watch_viedo1');
        const rewards = App.getCfg('reward'); 
        this.lblTips.string = App.Util.format(str,rewards[GameModule.isSingle?0:1]);

        const ranks = GameModule.rankInfo;
        if (ranks.length < 1){
            this.lblRank.string = `暂无排名信息`;
        } else {
            ranks.forEach((it,idx) => {
                if (it.UserID === data.UserID){
                    this.lblRank.string = `第 ${idx+1} 名`;
                    return ;
                }
        });
        }

        let calendar = cc.instantiate(this.calendarPrefab);
        this.calendarNode.addChild(calendar);
        calendar.getComponent(Calendar).init();
        calendar.scale = 0.7;

        App.Util.createRQcode(this.QRCodeNode,App.getCfg('official'),cc.color(49,146,113));
        
        // 注册红点
        RedPointSys.register(App.Util.find('buttomNode/btnVideo',this.node),cc.v2(68,50),0,[RedPointEvent.NOTIFY_ACTIVE]);
        RedPointSys.register(this.node.getChildByName('headNode'),cc.v2(0,0),0,[RedPointEvent.NOTIFY_ACTIVE]);
        RedPointSys.register(this.node.getChildByName('btnMail'),cc.v2(50,40),1,[RedPointEvent.NOTIFY_NUMBER]);
        RedPointSys.register(this.node.getChildByName('btnNotice'),cc.v2(50,40),1,[RedPointEvent.NOTIFY_NUMBER]);

        if (App.getCfg('test')){
            MailSys.updateMail();
            PackSys.updatePack();
        }

        App.openView(`Notice:hall`);
    }

    onClick(obj, flag) {
        RedPointSys.touchTarget(obj.target,false);
        if (flag === 'Rank' && GameModule.rankInfo.length < 1){
            App.popTips('暂无排名信息');
            return ;
        }
        let _viewArray = ['UserInfo', 'Set','Rank','Rule','Pack','Notice','Mail'];
        let _view = _viewArray.indexOf(flag);
        if (_view !== -1) {
            App.openView(`${_viewArray[_view]}:hall`);
        } else if (flag === 'official') {
            cc.sys.openURL(App.getCfg('official'));
        } else if (flag === 'share') {
            App.popTips(App.getText('developing'));
        } else if (flag === 'Video'){
            App.Event.emit(LandlordEvents.EC_WATCH_VIDEO);
        } else if (flag === 'official'){
        } else if (flag === 'match' || flag === 'ordinary'){
            if (flag === 'match'){
                App.popTips(App.getText(GameModule.isSingle ? 'landlord.single_match' : 'developing'));
                return ;
            }
            GameModule.entryRoom(null);
        }
    }
}